package com.companyname.projecttemplate.ws;

import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatusCode;
import org.springframework.http.server.ServerHttpRequest;
import org.springframework.http.server.ServerHttpResponse;
import org.springframework.stereotype.Component;
import org.springframework.web.socket.WebSocketHandler;
import org.springframework.web.socket.server.HandshakeInterceptor;

import com.base.core.head.dto.JwtBaseDTO;
import com.base.core.mvc.core.LoginManager;
import com.companyname.projecttemplate.constants.TestTokenConstants;

/**
 * 
 * @author start
 *
 */
@Component
public class AuthorityHandshakeInterceptor implements HandshakeInterceptor {
	
	@Autowired
	private LoginManager loginManager;
	
    @Override
    public boolean beforeHandshake(ServerHttpRequest request, ServerHttpResponse response, WebSocketHandler wsHandler, Map<String, Object> attributes) throws Exception {
    	try {
    		JwtBaseDTO jwt=loginManager.analysisJwt(JwtBaseDTO.class,false,TestTokenConstants.CUSTOMER_TOKEN);
        	attributes.put(AppWebSocketHandler.WEBSOCKET_TOKEN,jwt.getAccessId());
            return true;
    	}catch(Exception e) {
    		response.setStatusCode(HttpStatusCode.valueOf(401));
    	}
    	return false;
    }

    @Override
    public void afterHandshake(ServerHttpRequest request, ServerHttpResponse response, WebSocketHandler wsHandler, Exception exception) {

    }
    
}
